package controllers;

import dto.retjson.RetDataDto;
import models.application.AniApp;
import models.application.AniAppClassification;
import models.pubobj.AniObjectType;
import models.pubobj.AniObjectState;
import models.user.*;
import play.libs.Json;
import play.mvc.*;
import services.application.AniAppEditService;
import services.application.AniAppInstallService;
import services.application.AniAppQueryService;
import services.application.AniAppRegisterService;
import services.user.*;
import utils.*;

import java.util.*;

/**
 * User: yeqingzhou
 * Date: 12/23/12
 * Time: 10:45 PM
 */
public class AniAppController extends BaseController{

    // --register app
    public static Result registerApp(){
        try{
            AniUser curUser = AniLocalUserService.currentLoginUser();
            Map<String, String> appRegInfoArgs = MVCUtils.getRequestArgs(request());
            AniAppRegisterService.registerApp(appRegInfoArgs, curUser);
        }catch(Exception e){
            e.printStackTrace();
            return ok(Json.toJson(new RetDataDto(false, e.getMessage())));
        }
        return ok(Json.toJson(new RetDataDto()));
    }

    // --edit app
    public static Result updateApp(){
        try{
            AniUser curUser = AniLocalUserService.currentLoginUser();
            Map<String, String> appRegInfoArgs = MVCUtils.getRequestArgs(request());
            AniAppEditService.updateApp(appRegInfoArgs, curUser);
        }catch(Exception e){
            e.printStackTrace();
            return ok(Json.toJson(new RetDataDto(false, e.getMessage())));
        }
        return ok(Json.toJson(new RetDataDto()));
    }
    public static Result deleteApp(){
        try{
            AniUser curUser = AniLocalUserService.currentLoginUser();
            Map<String, String> delAppInfo = MVCUtils.getRequestArgs(request());
            AniAppEditService.deleteApp(delAppInfo, curUser);
        }catch (Exception e){
            e.printStackTrace();
            return ok(Json.toJson(new RetDataDto(false, e.getMessage())));
        }
        return ok(Json.toJson(new RetDataDto()));
    }
    // --switch app state
    public static Result switchAppState(){
        try{
            AniUser curUser = AniLocalUserService.currentLoginUser();
            AniAppEditService.switchAppEnable(
                    Long.parseLong(
                            MVCUtils.getRequestArgs(request()).get("appId")
                    ),
                    curUser
            );
        }catch (Exception e){
            e.printStackTrace();
            return ok(Json.toJson(new RetDataDto(false, e.getMessage())));
        }
        return ok(Json.toJson(new RetDataDto()));
    }
    // --install or remove app
    public static Result installOrRemoveApps(){
        String appIds = MVCUtils.getRequestArgs(request()).get("appIds");
        try{
            AniAppInstallService.installOrRemoveApps(
                    appIds,
                    AniLocalUserService.currentLoginUser()
            );
            return ok(
                    Json.toJson(
                            new RetDataDto(
                                    true,
                                    "ok"
                            )
                    )
            );
        } catch (Exception e){
            e.printStackTrace();
            return ok(Json.toJson(new RetDataDto(false, e.getMessage())));
        }
    }
    // --query app

    public static Result queryAppWithId(Long appId){
        //Long appId = Long.parseLong(MVCUtils.getRequestArgs(request()).get("appId"));
        try{
            return ok(
                    Json.toJson(
                            new RetDataDto(
                                    AniAppQueryService.queryAppWithId(
                                            appId,
                                            AniLocalUserService.currentLoginUser()
                                    )
                            )
                    )
            );
        }catch(Exception e){
            e.printStackTrace();
            return internalServerError(
                    Json.toJson(
                            new RetDataDto(
                                    false,
                                    e.getMessage()
                            )
                    )
            );
        }
    }
    public static Result queryPublicApps(
            Long classificationId,
            String keyword,
            Integer page
    ){
        try{
            return ok(
                    Json.toJson(
                            new RetDataDto(
                                    AniAppQueryService.queryPublicApps(
                                            AniLocalUserService.currentLoginUser(),
                                            new AniAppClassification(classificationId),
                                            keyword,
                                            page
                                    )
                            )
                    )
            );
        }catch(Exception e){
            e.printStackTrace();
            return internalServerError(
                    Json.toJson(
                            new RetDataDto(
                                    false,
                                    e.getMessage()
                            )
                    )
            );
        }
    }
    public static Result queryUserOwnedApps(
            Long classificationId,
            Long typeId,
            Long stateId,
            Long parentAppId,
            String keyword,
            Integer page
    ){
        try{
            return ok(
                    Json.toJson(
                            new RetDataDto(
                                    AniAppQueryService.queryUserOwnedApps(
                                            AniLocalUserService.currentLoginUser(),
                                            new AniAppClassification(classificationId),
                                            new AniObjectType(typeId),
                                            new AniObjectState(stateId),
                                            new AniApp(parentAppId),
                                            keyword,
                                            page
                                    )
                            )
                    )
            );
        }catch(Exception e){
            e.printStackTrace();
            return internalServerError(
                    Json.toJson(
                            new RetDataDto(
                                    false,
                                    e.getMessage()
                            )
                    )
            );
        }
    }
    public static Result queryUserInstalledApps(
            Long classificationId,
            Long parentAppId,
            String keyword,
            Integer page
    ){
        try{
            return ok(
                    Json.toJson(
                            new RetDataDto(
                                    AniAppQueryService.queryUserInstalledApps(
                                            AniLocalUserService.currentLoginUser(),
                                            new AniAppClassification(classificationId),
                                            new AniApp(parentAppId),
                                            keyword,
                                            page
                                    )
                            )
                    )
            );
        }catch(Exception e){
            e.printStackTrace();
            return internalServerError(
                    Json.toJson(
                            new RetDataDto(
                                    false,
                                    e.getMessage()
                            )
                    )
            );
        }
    }
}
